package com.hhxy.exception;

import java.util.Scanner;

public class AgeTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("程序开始……");
        System.out.println("请输入您的年龄：");
        try {//自定义的编译时异常会主动帮你判断
            check(sc.next());
        } catch (DefiniedException e) {
            e.printStackTrace();
        }
        System.out.println("程序结束……");
    }

    private static void check(String x) throws DefiniedException {
        int age = 0;
        try {
            age = Integer.parseInt(x);
            if (age < 0 || age > 200) {
                //用到了自定义异常，该异常是编译时异常，需要使用throws抛出异常
                throw new DefiniedException(age + " is illeagal!");
            } else {
                System.out.println("年龄合法");
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }
}
